package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.api.service.AgentServiceRpc;
import com.weixing.mall.provider.api.service.MemberServiceRpc;
import com.weixing.mall.provider.mapper.CustomerMessageMapper;
import com.weixing.mall.provider.mapper.MemberMapper;
import com.weixing.mall.provider.mapper.MemberMessageMapper;
import com.weixing.mall.provider.mapper.MessageTypeMapper;
import com.weixing.mall.provider.model.domain.CustomerMessage;
import com.weixing.mall.provider.model.domain.MemberMessage;
import com.weixing.mall.provider.model.dto.CustomerMessageDto;
import com.weixing.mall.provider.model.enums.MemberMsgTypeEnum;
import com.weixing.mall.provider.model.query.MemberMessageQuery;
import com.weixing.mall.provider.service.ICustomerMessageService;
import com.weixing.mall.provider.service.IMemberMessageService;
import com.weixing.mall.push.GtComponent;
import com.weixing.mall.push.PushMsg;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-25
 */
@Service
public class CustomerMessageServiceImpl extends ServiceImpl<CustomerMessageMapper, CustomerMessage> implements ICustomerMessageService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberMessageMapper memberMessageMapper;
    @Autowired
    private AgentServiceRpc agentServiceRpc;
    @Autowired
    private MessageTypeMapper messageTypeMapper;
    @Autowired
    private IMemberMessageService memberMessageService;
    @Autowired
    private GtComponent gtComponent;


    @Override
    public void save(CustomerMessageDto customerMessageDto,Long uid) {
        CustomerMessage customerMessage = new CustomerMessage();
        BeanUtils.copyProperties(customerMessageDto,customerMessage);
        customerMessage.setCreatedTime(new Date());
        customerMessage.setCreatedBy(uid);
        baseMapper.insert(customerMessage);

        /* 2020年4月11日18:36:41  修改消息发布方式
        //根据消息类别ID查询消息类别范围
        MessageType messageType = messageTypeMapper.selectById(customerMessageDto.getMsgType());
        //根据客服Id和代理商等级查询下级所属代理和会员
        MemberMessage memberMessage = new MemberMessage();
        BeanUtils.copyProperties(customerMessageDto,memberMessage);
        String[] split = messageType.getTypeRange().split(",");
        for (String s : split) {
            //如果为0 则根据客服Id查会员
            if(s.equals("0")){
                List<Long> ids=memberMapper.selectByCustomerId(uid);
                for (Long id : ids) {
                    memberMessage.setMemberId(id);
                    memberMessageMapper.insert(memberMessage);
                }
            }else {
                //否则根据等级和客服Id查询代理商
                List<Long> ids=agentServiceRpc.selectByCustomerIdAgentLevel(Integer.parseInt(s),uid);
                for (Long id : ids) {
                    memberMessage.setMemberId(id);
                    memberMessageMapper.insert(memberMessage);
                }
            }

        }*/
        //新会员消息是根据平台所选择的会员进行消息插入
        if(StrUtil.isNotEmpty(customerMessageDto.getAgentIds())){//插入私有消息
            List<MemberMessage> memberMessageList = Lists.newArrayList();
           // String[] memberIds = customerMessageDto.getMemberIds().split(",");
            String[] agentIds = customerMessageDto.getAgentIds().split(",");
            for (String agentId:agentIds){
                MemberMessage memberMessage = new MemberMessage();
                BeanUtils.copyProperties(customerMessageDto,memberMessage);
                //memberMessage.setMemberId(Long.valueOf(memberId));
                memberMessage.setAgentId(Long.valueOf(agentId));
                memberMessage.setMsgType(MemberMsgTypeEnum.TYPE2.getValue());
                memberMessage.setCreatedBy(uid);
                memberMessageList.add(memberMessage);
            }
            memberMessageService.saveBatch(memberMessageList);
            //批量推送消息
            List<String> cids = memberMapper.loadAgentCid(agentIds);
            PushMsg pushMsg = new PushMsg();
            pushMsg.setTitle(customerMessageDto.getTitle());
            pushMsg.setBody(customerMessageDto.getMsg());
            pushMsg.setClick_type("none");
            gtComponent.listCid(pushMsg,cids);
        }else{//若是公共消息 则插入一条公共消息类型的会员消息
            MemberMessage memberMessage = new MemberMessage();
            BeanUtils.copyProperties(customerMessageDto,memberMessage);
            memberMessage.setMsgType(MemberMsgTypeEnum.TYPE1.getValue());
            memberMessage.setCreatedBy(uid);
            memberMessageService.save(memberMessage);
            //广播推送消息
            PushMsg pushMsg = new PushMsg();
            pushMsg.setTitle(memberMessage.getTitle());
            pushMsg.setBody(memberMessage.getMsg());
            pushMsg.setClick_type("none");
            gtComponent.pushAll(pushMsg);
        }
    }

    @Override
    public IPage selectOfPage(Page initMpPage, MemberMessageQuery memberMessageQuery) {
        return baseMapper.selectOfPage(initMpPage,memberMessageQuery);
    }
}
